<template>
  <slot></slot>
  <div class="login">
    <div class="flex items-center justify-center w-100 login-content relative" :class="`login-type-3`">
      <!-- <div style="word-break: break-all; position: fixed;z-index: 98; background-color: #fff; top: 0; z-index: 9999;">用户信息 --- {{a}}</div> -->
      <el-form ref="formRef" :model="formData" label-width="0" :rules="formRules" class="w-100">
        <div class="title">绑定手机号<p class="tips">为保障账号安全，首次登录请绑定手机号</p></div>
        <el-form-item label="" prop="mobile" class="mobile-box">
          <div class="num">+86</div>
          <el-input v-model="formData.mobile" placeholder="请输入手机号" size="large"></el-input>
        </el-form-item>
        <el-form-item label="" prop="captcha" class="code-box">
          <el-input ref="codeInputRef" v-model="formData.captcha" type="text" placeholder="请输入验证码" size="large" maxlength="11"></el-input>
          <div class="code" @click="sendMsg">{{codetxt}}</div>
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" class="submit-btn" @click="submit" size="large" :disabled="!formData.mobile || !formData.captcha">绑定</el-button>
          <!-- <el-button type="primary" class="submit-btn" @click="$emit('confirm')">绑定</el-button> -->
        </el-form-item>
        <!-- <el-form-item label="">
          <el-button type="warning" class="submit-btn" @click="userStore.logout()" size="large">退出</el-button>
        </el-form-item> -->
      </el-form>
    </div>
  </div>
  
</template>

<script lang="ts" setup>
import { smsSendApi, bindmobileApi } from '@/api/app';
import { useUserStore } from '@/stores/user'
import { computed, reactive, ref } from 'vue';
import { isMobile } from '@/utils/util';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';
import { loginOk } from '@/utils/weChat'
// const a = ref('');
// userinfosApi().then(res=>{
//   a.value = JSON.stringify(res.userinfo)
// })
const userStore = useUserStore();
const router = useRouter()
// 手机验证码
const codetxt = computed(()=>{
  return codeTime.value === 0 ? '获取验证码' : `${codeTime.value}s 后重新获取`
});
const formData = reactive({
    mobile: '', // 
    captcha: '',
})
const formRules = {
  mobile: [
    { required: true, message: '请输入账号', trigger: ['change', 'blur'] }
  ],
  captcha: [
    { required: true, message: '请输入验证码', trigger: ['change', 'blur'] }
  ],
}
const codeRun = ref()
const codeTime = ref(0);
const codeInputRef = ref()
const sendMsg = () => {
  const regex = /^1[2-9]\d{9}$/;
  if(!formData.mobile) return ElMessage.error('请输入手机号');
  if(!regex.test(formData.mobile)) return ElMessage.error('手机号格式不正确');
  if (codeInputRef.value) {
    (codeInputRef.value.$el as HTMLElement).querySelector('input')?.focus();
  }
  smsSendApi({mobile: formData.mobile, event:'bindmobile'}).then(res=>{
    codeTime.value = 60
    codeRun.value = setInterval(()=>{
      codeTime.value--;
      if(codeTime.value == 0){
        clearInterval(codeRun.value)
      }
    }, 1000)
  })
}
const formRef = ref()
// 登录提交
const emits = defineEmits(['confirm'])
const submit = async () => {
  await formRef.value?.validate();
  const res = await bindmobileApi(formData)
  console.log(res)
  if(res){
    loginOk(res)
    ElMessage.success('登录成功~');
    console.log('手机号绑定好后---------', userStore.token);
    clearInterval(codeRun.value);
    // pc端的话 先去看看要不要完善简历
    if(!isMobile){
      emits('confirm')
    }else{
      router.push('/')
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/assets/styles/login.scss'; // 确保路径正确
</style>